package com.nineclock.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.attendance.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.mapper.SysDepartmentMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.pojo.SysDepartment;
import com.nineclock.system.service.SysDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysDepartmentServiceImpl implements SysDepartmentService {
    @Autowired
    SysDepartmentMapper departmentMapper;
    @Override
    public List<SysDepartmentDTO> queryDepartmentList() {
        //获取当前的企业id
        Long companyId = CurrentUserHolder.get().getCompanyId();
        List<SysDepartmentDTO> departmentList = this.queryChildrenDepartmentList(companyId, 0L);
        return departmentList;
    }

//    获取所有的顶级部门
    @Override
    public List<Long> queryTopDepartmentIds() {
        LambdaQueryWrapper<SysDepartment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDepartment::getCompanyId,CurrentUserHolder.get().getCompanyId());
        lambdaQueryWrapper.eq(SysDepartment::getId,0);
        List<SysDepartment> departmentList = departmentMapper.selectList(lambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(departmentList)){
            departmentList.stream().map(sysDepartment -> {
                return sysDepartment.getId();
            }).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Autowired
    SysCompanyUserMapper companyUserMapper;
    @Override
    public List<DepartmentOrUserSimpleDTO> querySimpleDepartment(Long departmentId, Integer includeMember) {
        List<DepartmentOrUserSimpleDTO> all = new ArrayList<>();

        // 查询departmentId 子部门
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getParentId, departmentId);
        wrapper.eq(SysDepartment::getCompanyId,CurrentUserHolder.get().getCompanyId());
        List<SysDepartment> departmentList = departmentMapper.selectList(wrapper);
        //把 部门列表转换为 返回值类型列表
        List<DepartmentOrUserSimpleDTO> departmentSimpleList = departmentList.stream().map(department -> {
            DepartmentOrUserSimpleDTO departmentSimpleDTO = new DepartmentOrUserSimpleDTO();
            departmentSimpleDTO.setType(1);
            departmentSimpleDTO.setId(department.getId());
            departmentSimpleDTO.setName(department.getName());
            return departmentSimpleDTO;
        }).collect(Collectors.toList());

        all.addAll(departmentSimpleList);

        //如果 includeMember为0 ，说明包含员工信息
        if( includeMember == 0){
            LambdaQueryWrapper<SysCompanyUser> companyUserQw = new LambdaQueryWrapper<>();
            companyUserQw.eq(SysCompanyUser::getCompanyId, CurrentUserHolder.get().getCompanyId());
            companyUserQw.eq(SysCompanyUser::getDepartmentId, departmentId);
            List<SysCompanyUser> companyUserList = companyUserMapper.selectList(companyUserQw);

            List<DepartmentOrUserSimpleDTO> userSimpleDTOList = companyUserList.stream().map(companyUser -> {
                DepartmentOrUserSimpleDTO userSimpleDTO = new DepartmentOrUserSimpleDTO();
                userSimpleDTO.setType(2);
                userSimpleDTO.setName(companyUser.getUserName());
                userSimpleDTO.setId(companyUser.getId());
                return userSimpleDTO;
            }).collect(Collectors.toList());

            all.addAll(userSimpleDTOList);
        }

        return all;
    }

    private List<SysDepartmentDTO> queryChildrenDepartmentList(Long companyId, long parentId) {
        //查询该父类目录下所有的一级子目录
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getParentId,parentId);
        wrapper.eq(SysDepartment::getCompanyId,companyId);
        List<SysDepartment> sysDepartments = departmentMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(sysDepartments)){
            List<SysDepartmentDTO> departmentDTOList = BeanHelper.copyWithCollection(sysDepartments, SysDepartmentDTO.class);
            for (SysDepartmentDTO sysDepartmentDTO : departmentDTOList) {
                //获取 子部门的子部门列表
                List<SysDepartmentDTO> childDepartmentDTOList = this.queryChildrenDepartmentList(companyId,sysDepartmentDTO.getId());
                sysDepartmentDTO.setChildren(childDepartmentDTOList);
            }

            return departmentDTOList;
        }
        return new ArrayList<>();
    }
}
